class BrowserHistory {
    constructor(homepage) {
        this.history = [homepage]; // 存储所有访问的页面
        this.current = 0; // 当前页面的索引
    }

    visit(url) {
        // 删除当前页面之后的所有历史记录
        this.history = this.history.slice(0, this.current + 1);
        // 添加新访问的页面
        this.history.push(url);
        // 更新当前页面索引
        this.current = this.history.length - 1;
    }

    back(steps) {
        // 计算最多能后退的步数
        const backSteps = Math.min(steps, this.current);
        // 更新当前页面索引
        this.current -= backSteps;
        // 返回后退后的页面
        return this.history[this.current];
    }

    forward(steps) {
        // 计算最多能前进的步数
        const forwardSteps = Math.min(steps, this.history.length - 1 - this.current);
        // 更新当前页面索引
        this.current += forwardSteps;
        // 返回前进后的页面
        return this.history[this.current];
    }
}

// 测试示例
const browserHistory = new BrowserHistory("leetcode.com");
console.log(browserHistory.visit("google.com"));       // null
console.log(browserHistory.visit("facebook.com"));     // null
console.log(browserHistory.visit("youtube.com"));      // null
console.log(browserHistory.back(1));                   // "facebook.com"
console.log(browserHistory.back(1));                   // "google.com"
console.log(browserHistory.forward(1));                // "facebook.com"
console.log(browserHistory.visit("linkedin.com"));     // null
console.log(browserHistory.forward(2));                // "linkedin.com"
console.log(browserHistory.back(2));                   // "google.com"
console.log(browserHistory.back(7));                   // "leetcode.com"